package action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;

import dao.Users;
import dao.UsersDAO;

public class UserAction implements ModelDriven<Users>{
	private Users user;
	private UsersDAO usersdao;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private HttpServletResponse response = ServletActionContext.getResponse();
	public String login() throws Exception {
		String name =user.getUsername();
		String psw = user.getPassword();
		boolean u = new UsersDAO().get(name,psw);
		if(u == true) {
			Users user=new UsersDAO().getUser(name, psw);
			request.getSession().setAttribute("UserMessage", user);
			return "success";
			
		}else {
			request.setAttribute("error", "用户名或密码错误");
			return "error";
		}
	}
	
	public String insert() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name1=request.getParameter("username");
		String name2=request.getParameter("nickname");
		String psw=request.getParameter("password");
		boolean f = new UsersDAO().getUser(name1);
		if(f==false) {
		boolean u=new UsersDAO().insert(name1,name2, psw);
			if (u == true) {
				request.setAttribute("error", "注册成功");
				request.setAttribute("username", name1);
				request.setAttribute("nickname", name2);
				request.setAttribute("password", psw);
				return "success";
			}
			else {
				request.setAttribute("error", "注册失败");
				return "error";
			}
		}else {
			request.setAttribute("error", "该用户名已被注册");
			return "error";
		}
	}
	
	public String getUser() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name=request.getParameter("name");
		List<Users> u=new UsersDAO().get(name);
		if (u !=null) {			
			request.setAttribute("list",u);
			return "success";
		}
		else {
			request.setAttribute("error", "查询失败");
			return "error";
		}
	}
	
	public String getAll() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		List<Users> u=new UsersDAO().getAll();
		if (u !=null) {			
			request.setAttribute("list",u);
			return "success";
		}
		else {
			request.setAttribute("error", "查询失败");
			return "error";
		}
	}
	
	public String update() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		Users obj=(Users)request.getSession().getAttribute("UserMessage");
		String name1 =obj.getUsername();
		String name2 =request.getParameter("nickname");
		String psw =request.getParameter("password");
		boolean u = new UsersDAO().update(name1,name2,psw);
		if (u == true) {
			request.setAttribute("error", "修改成功");
			obj=new UsersDAO().getUser(name1, psw);
			request.getSession().setAttribute("UserMessage", obj);
			return "success";
		}
		else {
			request.setAttribute("error", "修改失败");
			return "error";
		}
	}
	
	public String delete() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		Users obj=(Users)request.getSession().getAttribute("UserMessage");
		String name=obj.getUsername();
		boolean u=new UsersDAO().delete(name);
		if (u ==true) {			
			request.setAttribute("error","删除成功");
			return "success";
		}
		else {
			request.setAttribute("error", "无法删除");
			return "error";
		}
	}
	
	public String quit() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.getSession().removeAttribute("UserMessage");		
		request.setAttribute("error","已退出");
		return "success";
	}
	
	@Override
	public Users getModel() {
		// TODO Auto-generated method stub
		user=new Users();
		return this.user;
	}
}
